package com.mdsi.teacher;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mdsi.models.ConnectionManager;
import com.mdsi.models.Subject;
import com.mdsi.models.Teacher;

/**
 * Servlet implementation class SubjectList
 */
@WebServlet("/User/Teacher/SubjectList")
public class SubjectList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SubjectList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Connection conn = ConnectionManager.getConnection() ;
			ResultSet result = conn
				.prepareStatement("SELECT DISTINCT L.SUBJECT.ID_SUBJECT, L.SUBJECT.NAME FROM LESSON L WHERE L.TEACHER.EMAIL='" 
						+ ((Teacher)request.getSession().getAttribute("user")).getEmail()
						+ "'")
				.executeQuery() ;
			ArrayList <Subject> subjects = new ArrayList <Subject> () ;
			while (result.next()) {
				subjects.add(new Subject(result.getLong(1), result.getString(2))) ;
			}
			getServletContext().setAttribute("subjects", subjects) ;
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		this.getServletContext().getRequestDispatcher("/WEB-INF/teacher/subject_list.jsp").forward(request, response);		
	}

}
